unit uAbstractDataModule;

interface

uses
  SysUtils, Classes,FMTBcd,  DBClient, Provider, DB, SqlExpr,Dialogs,
  Controls,uRecError,uMainDM,Variants,Forms;

type
  TAbstractDataModule = class(TDataModule)
    SqlControl: TSQLDataSet;
    dspControl: TDataSetProvider;
    cdsControl: TClientDataSet;
    procedure cdsApplyUpdates(DataSet:TDataSet);
    procedure cdsCancelUpdates(DataSet: TDataSet);
    procedure cdsControlBeforeDelete(DataSet: TDataSet);
    procedure cdsControlBeforeRefresh(DataSet: TDataSet);
    procedure cdsReconcileError(DataSet: TCustomClientDataSet;
                                E: EReconcileError;
                                UpdateKind: TUpdateKind;
                                var Action: TReconcileAction);
    procedure DataModuleCreate(Sender: TObject);
  private
    FDBConnection: TSQLConnection;
    procedure SetDBConnection(const Value: TSQLConnection);
    { Private declarations }
  public
    { Public declarations }
    property DBConnection:TSQLConnection read FDBConnection write SetDBConnection;
    procedure  CheckRequiredsFields(DataSet:TDataSet);
    function GenerateID(Gen:String):Integer;
  end;




implementation
 uses uMainForm;
{$R *.dfm}

procedure TAbstractDataModule.cdsApplyUpdates(DataSet: TDataSet);
begin
  if TClientDataSet(DataSet).ChangeCount > 0 then
    TClientDataSet(DataSet).ApplyUpdates(0);
end;

procedure TAbstractDataModule.cdsCancelUpdates(DataSet: TDataSet);
  begin
    if TClientDataSet(DataSet).ChangeCount>0 then
      TClientDataSet(DataSet).CancelUpdates;
  end;
procedure TAbstractDataModule.cdsControlBeforeDelete(DataSet: TDataSet);
begin
  if MessageDlg('Delete record?',mtConfirmation,[mbYes,mbNo],0,mbYes)=mrNo then
    raise Exception.Create('Caceled');
end;

procedure TAbstractDataModule.cdsControlBeforeRefresh(DataSet: TDataSet);
begin
  if TClientDataSet(DataSet).ChangeCount>0 then
    TClientDataSet(DataSet).CancelUpdates;
end;

procedure TAbstractDataModule.cdsReconcileError(DataSet: TCustomClientDataSet;
  E: EReconcileError; UpdateKind: TUpdateKind; var Action: TReconcileAction);
begin
  Action := HandleReconcileError(DataSet,UpdateKind,E);
end;

procedure TAbstractDataModule.CheckRequiredsFields(DataSet: TDataSet);
var
 i:Integer;
begin
  for i:=0 to DataSet.FieldCount-1 do
    if DataSet.Fields[i].Required then
      if Trim (VarToStr(DataSet.Fields[i].Value))='' then
        raise Exception.Create('Field '''+DataSet.Fields[i].DisplayLabel+' '' is required!');


end;

procedure TAbstractDataModule.DataModuleCreate(Sender: TObject);
var
  i: Integer;
begin
   for i := 0 to ComponentCount-1 do
    if Components[i] is TClientDataSet then
     TClientDataSet(Components[i]).OnReconcileError:=cdsReconcileError;
end;

function TAbstractDataModule.GenerateID(Gen: String): Integer;
var
  sqlID:TSQLQuery;
begin
  sqlID:=TSQLQuery.Create(nil);
  try
      sqlID.SQLConnection:=TMainForm(Application.MainForm).DBConnection;;

      sqlID.SQL.Add('SELECT GEN_ID('+Gen+',1) As nID');
      sqlID.SQL.Add('   FROM RDB$DATABASE');
      sqlID.Open;

      if sqlID.RecordCount > 0 then
       Result := sqlID.FieldByName('nID').AsInteger
      else
        Result:=-1;
  finally
    sqlID.Free;
  end;
end;

procedure TAbstractDataModule.SetDBConnection(const Value: TSQLConnection);
var
  i:Integer;
begin
  FDBConnection := Value;
  for i:=0 to ComponentCount-1 do
    if Components[i] is TSQLDataSet then
      if not TSQLDataSet(Components[i]).Active then
        TSQLDataSet(Components[i]).SQLConnection:=fDBConnection;

end;

end.
